package com.liuwei.desktop.app;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import com.liuwei.desktop.common.DBUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class APPInitializer {
	private static HashMap<String,String> AppProperties = new HashMap<>();
	
	public static HashMap<String, String> getAppProperties() {
		return AppProperties;
	}
	
	public static void launch() throws Exception{
    	//加载配置文件
		String baseDir = System.getProperty("user.dir");
    	String appPropertyPath = baseDir+File.separator+"resource"+File.separator+"app.properties";
    	String sqliteDBPath = baseDir+File.separator+"resource"+File.separator+"sqlite.db";
    	System.out.println(appPropertyPath);
    	loadProperties(appPropertyPath);
    	
    	//加载spring容器
    	String sqlLiteOnOff = AppProperties.get("sqlite.onoff");
    	if("on".equals(sqlLiteOnOff)){
    		String sqlDriver = AppProperties.get("sqlite.driver");
    		String sqlUrl = AppProperties.get("sqlite.url").replace("#", sqliteDBPath);
    		System.out.println(sqlUrl);
    		DBUtils.setSqlClass(sqlDriver);
    		DBUtils.setSqlUrl(sqlUrl);
    	}
    }
	
	private static void loadProperties(String path) throws IOException {
		InputStream is = new FileInputStream(new File(path));
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		Properties props = new Properties();
		props.load(br);
		for (Object key : props.keySet()) {
			String value = (String) props.get(key);
			AppProperties.put((String) key, value);
			System.out.println(key);
		}
	}
}
